package olSystem;

import com.spm.utils.StudentMapperUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

@WebServlet(name = "LoginsServlet", value = "/LoginsServlet")
public class LoginsServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    String message="";//定义一个变量
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request,response);
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String id = request.getParameter("id");
        String password = request.getParameter("password");
        //得到HttpSession类型的对象
        //HttpSession session = request.getSession();
       // StudentService stu=new StudentService();
        //String Id=(String) session.getAttribute("id");
        //String pwd=(String) session.getAttribute("password");
//        if (Id.equals(id) && pwd.equals(password)) {
//            response.sendRedirect("http://localhost:8080/cjw_war_exploded/Olsystem/logok.jsp");
//        } else {
//            message = "密码错误";
//            pr.println(message);
//            response.sendRedirect("http://localhost:8080/cjw_war_exploded/Olsystem/login.jsp");
//        }
//        DBOper db = new DBOper();
//        try {
//            db.getConnection();
//        } catch (SQLException e1) {
//            // TODO Auto-generated catch block
//            e1.printStackTrace();
//        }
        //String sql = "SELECT Sid,Spassword FROM student WHERE Sid = ? AND Spassword = ?";
//        String sql = "SELECT Sno,Spassword FROM student WHERE Sno = ? AND Spassword = ?";
//        ResultSet rs = db.executeQuery(sql,new String[]{id,password});
            StudentMapperUtil studentMapperUtil = new StudentMapperUtil();
            if(studentMapperUtil.selectIdAndPassword(id,password)==1){
                HttpSession session = request.getSession();

                session.setAttribute("numSession", id);
                Cookie cookie = new Cookie("stuno",id);
                cookie.setMaxAge(60*60*24*30);
                response.addCookie(cookie);
                //登录成功，跳转...
                //response.sendRedirect("http://localhost:8080/cjw_war_exploded/Olsystem/logok.jsp");
                RequestDispatcher dispatcher = request.getRequestDispatcher("FindServlet");
                dispatcher.forward(request,response);
            }else{
                out.print("登录失败");
                out.print("<br><a href = 'login.jsp'>重新登陆</a>");
//                response.setContentType("text/html;charset=UTF-8");
//
//                response.setHeader("refresh", "0; url=/StudentSelect");

            }
    }

    public void init()throws ServletException {

    }

}





